﻿using System;
using System.Data;
using System.Text;


namespace WinFormClient
{
    public class Dao
    {
        private string _tableName;
        private MethodCollection _methods;

        public Dao(string tableName, MethodCollection col)
        {
            this._tableName = tableName;
            this._methods = col;
        }

        public void Builder()
        {
            string content
            = this.Header()
            + this.Methods()
            + this.Footer()
            ;

            Tools.TextSaveToFile(content, "Dao", _tableName + ".cs");
        }

        #region header
        protected string Header()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("#region namespace" + Environment.NewLine);
            sb.Append("using System;" + Environment.NewLine);
            sb.Append("using System.Collections.Generic;" + Environment.NewLine);
            sb.Append("using System.Text;" + Environment.NewLine);
            sb.Append("using IBatisNet.DataAccess;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Interface;" + Environment.NewLine);
            sb.Append("using " + MapXml.NameSpace + ".Core;" + Environment.NewLine);
            sb.Append("#endregion" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("namespace " + MapXml.NameSpace + ".Dao" + Environment.NewLine);
            sb.Append("{" + Environment.NewLine);
            sb.Append("    partial class " + this._tableName + ":ObjectBase, Interface.I" + this._tableName + Environment.NewLine);
            sb.Append("    {" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        #region methods
        protected string Methods()
        {
            StringBuilder sb = new StringBuilder();
            Method mth = null;
            for (int i = 0; i < this._methods.Count;i++ )
            {
                mth = this._methods[i];
                sb.Append(Environment.NewLine);
                sb.Append("        public ");
                if (mth.AccessMethod == DataAccessMethod.ExecuteQueryForList)
                {
                    sb.Append("IList<" + mth.ReturnType + ">");
                }
                else
                {
                    sb.Append(mth.ReturnType);
                }

                sb.Append(" " + mth.MethodName + "(" + ((!String.IsNullOrEmpty(mth.ParameterType)) ? (mth.ParameterType + " " + mth.ParameterName) : "") + ")" + Environment.NewLine);
                sb.Append("        {" + Environment.NewLine);

                sb.Append("            return ");
                sb.Append("this." + mth.AccessMethod.ToString());
                if (mth.AccessMethod == DataAccessMethod.ExecuteQueryForList || mth.AccessMethod == DataAccessMethod.ExecuteQueryForObject) sb.Append("<" + mth.ReturnType + ">");
                sb.Append("(\"" + mth.StatementName + "\",");
                sb.Append(((!String.IsNullOrEmpty(mth.ParameterType))?mth.ParameterName : "null") + ");" + Environment.NewLine);

                sb.Append("        }" + Environment.NewLine);
                sb.Append(Environment.NewLine);
            }

            return sb.ToString();
        }
        #endregion

        #region footer
        protected string Footer()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("    }" + Environment.NewLine);
            sb.Append("}" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion

        public void Dispose()
        {
            this._methods.Dispose();
        }
    }
}
